import { fileURLToPath, URL } from 'node:url'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'

import basicSsl from '@vitejs/plugin-basic-ssl' //ssl

import legacy from '@vitejs/plugin-legacy'
// console.log(import.meta.env)
const Timestamp = new Date().getTime()
export default ({ command, mode }: any) => {
  return defineConfig({
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    },
    plugins: [
      vue(),
      vueJsx(),
      basicSsl(),
      legacy({
        targets: ['defaults', 'not IE 11', 'chrome 52', 'chrome 72'], //需要兼容的目标列表，可以设置多个
        additionalLegacyPolyfills: ['regenerator-runtime/runtime'],
        renderLegacyChunks: true,
        polyfills: [
          'es.symbol',
          'es.array.filter',
          'es.promise',
          'es.promise.finally',
          'es/map',
          'es/set',
          'es.array.for-each',
          'es.object.define-properties',
          'es.object.define-property',
          'es.object.get-own-property-descriptor',
          'es.object.get-own-property-descriptors',
          'es.object.keys',
          'es.object.to-string',
          'web.dom-collections.for-each',
          'esnext.global-this',
          'esnext.string.match-all'
        ]
      })
    ],
    base: './',
    build: {
      outDir: loadEnv(mode, process.cwd()).VITE_outputDir + Timestamp,
      assetsDir: 'assets',
      sourcemap: true // 或者使用 'inline' 等其他选项
    },
    server: {
      host: '0.0.0.0',
      port: 8991,
      https: false,
      proxy: {
        [loadEnv(mode, process.cwd()).VITE_APP_BASE_URL]: {
          target: 'http://10.8.24.118:5000',
          changeOrigin: true,
          rewrite: (path) =>
            path.replace(new RegExp(`^${loadEnv(mode, process.cwd()).VITE_APP_BASE_URL}`), '')
        },
        [loadEnv(mode, process.cwd()).VITE_APP_FILE_URL]: {
          target: 'http://10.8.24.118:5000',
          changeOrigin: true,
          rewrite: (path) =>
            path.replace(new RegExp(`^${loadEnv(mode, process.cwd()).VITE_APP_FILE_URL}`), '')
        }
      }
    }
  })
}
